﻿using System;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Test.Data.Doubles;

namespace Nvigorate.Test.Data.Doubles
{
    public class TestDbCommand : IDbCommand
    {
        public TestDbCommand(string commandText)
        {
            CommandText = commandText;
            Parameters = new TestDataParameterCollection();
        }

        #region Public Properties

        public int ResultCount { get; set; }
        public int ExecuteNonQueryCount { get; set; }
        public int ExecuteScalarCount { get; set; }
        public DataTable ResultTable { get; set; }
        public DataSet ResultSet { get; set; }

        #endregion

        #region Public Methods

        public IDataParameterCollection Parameters { get; set; }

        public object GetParameterValue(string parameterName)
        {
            var parameter = Parameters[parameterName] as IDataParameter;
            return parameter == null ? null : parameter.Value;
        }

        public void Dispose()
        {
        }

        public void Prepare()
        {

        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public IDbDataParameter CreateParameter()
        {
            throw new NotImplementedException();
        }

        public int ExecuteNonQuery()
        {
            ExecuteNonQueryCount++;
            return ResultCount;
        }

        public IDataReader ExecuteReader()
        {
            if (ResultSet == null && ResultTable == null)
            {
                throw new AssertFailedException("ExecuteReader was called, but DataTable and DataSet are undefined.");
            }

            DataSet ds;
            if (ResultTable != null)
            {
                ds = new DataSet();
                ds.Tables.Add(ResultTable);
            }
            else
            {
                ds = ResultSet;
            }

            var tables = new DataTable[ds.Tables.Count];
            ds.Tables.CopyTo(tables, 0);

            return new DataTableReader(tables);
        }

        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            throw new NotImplementedException();
        }

        public object ExecuteScalar()
        {
            ExecuteScalarCount++;
            return ResultCount;
        }

        public IDbConnection Connection { get; set; }

        public IDbTransaction Transaction
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public string CommandText { get; set; }

        public int CommandTimeout
        {
            get; set;
        }

        public CommandType CommandType
        {
            get; set;
        }

        public UpdateRowSource UpdatedRowSource
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion

        #region Nested Types

        #endregion
    }
}